package cn.hdy.org.service.impl;

import cn.hdy.base.service.impl.BaseServiceImpl;
import cn.hdy.base.util.PageList;
import cn.hdy.org.domain.Department;
import cn.hdy.org.mapper.DepartmentMapper;
import cn.hdy.org.query.DepartmentQuery;
import cn.hdy.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@CacheConfig(cacheNames = "department")
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private IDepartmentService departmentService;
    @Override
    @CacheEvict(key = "'List'")
    public void add(Department department) {
        departmentMapper.add(department);//添加部门并返回主键id
        String dirPath = "/"+department.getId();//获取id并拼接
        Department department1=null;
        if (department.getParent_id()!=null){
            //父级部门id不为空，查询父级部门
           department1 = departmentMapper.findOne(department.getParent_id());
        }
        if (department1!=null) {
            //父级部门存在，拼接上级目录
            dirPath = department1.getDirPath() + dirPath;
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @CacheEvict(key = "'List'")
    public void del(Long id) {
        departmentMapper.del(id);
    }

    @Override
    @CacheEvict(key = "'List'")
    public void patchDel(Long[] ids) {
        departmentMapper.patchDel(ids);
    }

    @Override
    @CacheEvict(key = "'List'")
    public void update(Department department) {
        String dirPath = "/"+department.getId();
        Department department1=null;
        if (department.getParent_id()!=null){
            department1 = departmentMapper.findOne(department.getParent_id());
        }
        if (department1!=null) {
            dirPath = department1.getDirPath() + dirPath;
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }
    @Override
    @Cacheable(key = "'List'")
    public List<Department> findAll() {
        return  departmentMapper.findAll();
    }



    @Override
    public PageList<Department> queryPage(DepartmentQuery query) {
        PageList<Department> pageList = new PageList<>();
        Long count = departmentMapper.queryCount(query);
        if (count == null || count == 0) {
            return pageList;
        }
        List<Department> departmentList = departmentMapper.queryData(query);
        pageList.setData(departmentList);
        pageList.setTotals(count);
        return pageList;
    }

    @Override
    public List<Department> deptTree(Long id) {
        //查询所有部门信息
        List<Department> departmentList = departmentService.findAll();
        //转为map集合，键id，值Department对象
        Map<Long,Department> departmentMap  = departmentList.stream().collect(
                Collectors.toMap(Department ::getId,d->d)//遍历
        );
        //前端需要得到的集合数据 - 部门树
        List<Department> deptTree = new ArrayList<Department>();
        for (Department department:departmentList){//遍历
            if (department.getId().equals(id))continue;//相同跳过
            if(department.getParent_id() == null){//没有父级部门，则为顶级部门
                deptTree.add(department);//添加到部门集合里
            }else{
                Department parent = departmentMap.get(department.getParent_id());//有父级部门，则获取父级部门
                parent.getChildren().add(department);//父级部门添加子部门
            }
        }
        return deptTree;
    }
}
